package cn.market.service.ad.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.persistence.Query;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.type.Type;
import org.springframework.stereotype.Service;

import cn.market.bean.QueryResult;
import cn.market.bean.ad.Advertisement;
import cn.market.bean.ad.HtmlModule;
import cn.market.bean.user.Market;
import cn.market.service.ad.AdvertisementService;
import cn.market.service.base.DaoSupport;
import cn.market.utils.Log;
import cn.market.utils.Util;
@Service
public class AdvertisementServiceBean extends DaoSupport<Advertisement> implements AdvertisementService {
	private String TAG = "AdvertisementServiceBean";
	@SuppressWarnings("unchecked")
	@Override
	public Advertisement getAdvertisementById(Integer id) {
		String sql = "select a.adId,a.name,a.content,a.clickCount,a.createDate,a.validTime,a.visible,a.recommend,a.belongToMarket.id,a.module.modulId,a.startDate "+//,h.modulId,h.name,m.id,m.name " +
				" from Advertisement a "+//,HtmlModule h,Market m " +
				" where a.adId=?0";
		//SQLQuery query = super.getSession().createSQLQuery(sql);
		Query query = super.em.createQuery(sql);
		query.setParameter(0, id);
		List list = query.getResultList();
		Advertisement ad = new Advertisement();
		if(list != null && list.size()>0){
			Object [] obj = (Object[])list.get(0);
			ad.setAdId(new Integer(String.valueOf(obj[0])));
			ad.setName(String.valueOf(obj[1]));
			ad.setContent(String.valueOf(obj[2]));
			ad.setClickCount(new Integer(String.valueOf(obj[3])));
			ad.setCreateDate(Util.getDate(String.valueOf(obj[4])));
			ad.setCreateDateStr(String.valueOf(obj[4]));
			ad.setValidTime(new Integer(String.valueOf(obj[5])));
			ad.setVisible(Boolean.valueOf(String.valueOf(obj[6])));
			ad.setRecommend(Boolean.valueOf(String.valueOf(obj[7])));
			if(obj[8] != null && !"".equals(String.valueOf(obj[8]).trim())){
				Market market = new Market();
				market.setId(new Integer(String.valueOf(obj[8])));
				//market.setName(String.valueOf(obj[11]));
				ad.setBelongToMarket(market);
			}
			if(obj[9] != null && !"".equals(String.valueOf(obj[9]).trim())){
				HtmlModule module = new HtmlModule();
				module.setModulId(new Integer(String.valueOf(obj[9])));
				//module.setName(String.valueOf(obj[9]));
				ad.setModule(module);
			}
			ad.setStartDate(Util.getDate(String.valueOf(obj[10])));
			ad.setStartDateStr(String.valueOf(obj[10]));
			if(ad.getBelongToMarket() != null){
				String sql_market = "select name from market where id=:id";
				SQLQuery query_market = super.getSession().createSQLQuery(sql_market);
				query_market.setParameter("id", ad.getBelongToMarket().getId());
				List market_list = query_market.list();
				if(market_list != null && market_list.size()>0){
					ad.getBelongToMarket().setName(String.valueOf(market_list.get(0)));
				}
			}
			if(ad.getModule() != null ){
				String sql_module = "select name from htmlmodule where modulId=:modulId";
				SQLQuery query_module = super.getSession().createSQLQuery(sql_module);
				query_module.setParameter("modulId", ad.getModule().getModulId());
				List module_list = query_module.list();
				if(module_list != null && module_list.size()>0){
					ad.getModule().setName(String.valueOf(module_list.get(0)));
				}
			}
		}
		return ad;
	}

	@SuppressWarnings("unchecked")
	@Override
	public QueryResult<Advertisement> getAllAdvertisements(String where,
			int first, int max, Object[] params,
			LinkedHashMap<String, String> orderby) {
		String methodName ="getAllAdvertisements() ";
		String sql = "select adId,name,clickCount,createDate,validTime,visible,recommend,startDate " +
				" from advertisement o " +where+super.buildOrderby(orderby);
		SQLQuery query = super.getSession().createSQLQuery(sql);
		if(params !=null&&params.length>0){
			for(int i=0;i<params.length;i++)
				query.setParameter(i+1, params[i]);
		}
		if(first != -1 && max != -1)
			query.setFirstResult(first).setMaxResults(max);
		QueryResult<Advertisement> qr = new QueryResult<Advertisement> ();
		List<Advertisement> ads = new ArrayList<Advertisement>();
		List list = query.list();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Object [] obj = (Object[])list.get(0);
				Advertisement ad = new Advertisement();
				ad.setAdId(new Integer(String.valueOf(obj[0])));
				ad.setName(String.valueOf(obj[1]));
				ad.setClickCount(new Integer(String.valueOf(obj[2])));
				ad.setCreateDate(Util.getDate(String.valueOf(obj[3])));
				ad.setCreateDateStr(String.valueOf(obj[3]));
				//ad.getCreateDate().
				Log.println(TAG, methodName+" -- the createdate is:"+String.valueOf(obj[3]));
				ad.setValidTime(new Integer(String.valueOf(obj[4])));
				ad.setVisible(Boolean.valueOf(String.valueOf(obj[5])));
				ad.setRecommend(Boolean.valueOf(String.valueOf(obj[6])));
				ad.setStartDate(Util.getDate(String.valueOf(obj[7])));
				ad.setStartDateStr(String.valueOf(obj[7]));
				ads.add(ad);
			}
		}
		qr.setResultlist(ads);
		qr.setTotalrecord(super.getCount());
		return qr;
	}

	@Override
	public void updateAdvertisement(Advertisement ad) {
		String sql = "update advertisement " +
				" set name=:1,content=:2,validTime=:3 " +
				" where adId=:4";
		SQLQuery query = super.getSession().createSQLQuery(sql);
		query.setParameter("1", ad.getName());
		query.setParameter("2", ad.getContent());
		query.setParameter("3", ad.getValidTime());
		query.setParameter("4", ad.getAdId());
		query.executeUpdate();
	}

	@Override
	public void updateAdvertisementBelongHtmlModule(Advertisement ad) {
		
	}

	@Override
	public void updateAdvertisementBelongMarket(Advertisement ad) {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public String getAddContentByHtmlModuleId(Integer moduleId) {
		String sql = "select content from Advertisement a where a.module.modulId=?0";
		//SQLQuery sqlquery = super.getSession().createSQLQuery(sql);
		Query query = em.createQuery(sql);
		query.setParameter(0, moduleId);
		//query.addScalar("content", Hibernate.BLOB);
		List list = query.getResultList();
		if(list != null && list.size()>0)
			return String.valueOf(list.get(0));
		return null;
	}

	
}
